<template>
  <a-modal v-model="visible" title="批次分单" width="80%" :maskClosable="false">
    <template slot="footer" class="modelBtn">
      <a-button @click="visible = false">取消</a-button>
      <a-button
        type="primary"
        @click="handleOk('12')"
        :loading="loading"
        v-if="batchState === '2'"
        :disabled="batchTableData.length === 0"
        >完成分单</a-button
      >
      <a-button
        type="primary"
        @click="handleOk('22')"
        :disabled="batchTableData.length === 0"
        :loading="loading"
        v-if="batchState === '12'"
        >完成拣货</a-button
      >
      <a-button
        type="primary"
        @click="handleOk('32')"
        :disabled="batchTableData.length === 0"
        :loading="loading"
        v-if="batchState === '22'"
        >完成打单</a-button
      >
      <a-button
        type="primary"
        @click="handleOk('42')"
        :disabled="batchTableData.length === 0"
        :loading="loading"
        v-if="batchState === '32'"
        >确认交运</a-button
      >
      <a-button
        type="primary"
        @click="handleOk('52')"
        :disabled="batchTableData.length === 0"
        :loading="loading"
        v-if="batchState === '42'"
        >确认发货</a-button
      >
    </template>
    <a-table
      size="small"
      :columns="columns"
      :data-source="batchTableData"
      :pagination="false"
      :scroll="{ x: 1200, y: 1200 }"
    >
      <span style="color: #3eae7c" slot="applyState" slot-scope="text, record">
        {{ record.applyState }}
      </span>
      <span slot="waybillNo" slot-scope="text, record">
        {{ record.ectnNo===null?record.waybillNo:record.ectnNo }}
      </span>
    </a-table>
  </a-modal>
</template>

<script>
import {
  OutBound_batchManageApply,
  OutBound_batchManageState,
} from "@/apis/system/WM/OutOrder";
export default {
  props: ["searchData", "batchState", "getBatchTableList"],
  data() {
    return {
      loading: false,
      visible: false,
      batchTableData: [],
      modalState: "",
      rowId: "",
    };
  },
  mounted() {
    this.$bus.$off("BatchManageModal");
    this.$bus.$on("BatchManageModal", async (rowData, state) => {
      this.visible = true;
      this.modalState = state;
      this.rowId = rowData.key;
      let { code, responseBody } = await OutBound_batchManageApply(
        rowData.bachNo
      );
      if (code === "200") {
        this.batchTableData = [...responseBody];
        this.batchTableData.forEach((item, i) => {
          item.IDX = i + this.searchData.current * this.searchData.size - 9;
          item.createTime = item.createTime
            ? item.createTime.replace("T", "  ")
            : "";
          if (item.applyState === "5") {
            item.applyState = "待提交";
          } else if (item.applyState === "15") {
            item.applyState = "待仓库处理";
          } else if (item.applyState === "25") {
            item.applyState = "正在处理";
          } else if (item.applyState === "35") {
            item.applyState = "已发货";
          } else if (item.applyState === "45") {
            item.applyState = "订单异常";
          } else if (item.applyState === "55") {
            item.applyState = "暂缓处理";
          }
        });
      }
    });
  },
  methods: {
    //交运按钮
    handleOk(flag) {
      this.loading = true;
      if (flag === "12") {
        setTimeout(async () => {
          let { code } = await OutBound_batchManageState(`${this.rowId}/12`);
          if (code === "10000") {
            this.loading = false;
            this.visible = false;
            this.getBatchTableList();
          } else {
            this.loading = false;
            return false;
          }
        }, 1000);
      } else if (flag === "22") {
        setTimeout(async () => {
          let { code } = await OutBound_batchManageState(`${this.rowId}/22`);
          if (code === "10000") {
            this.loading = false;
            this.visible = false;
            this.getBatchTableList();
          } else {
            this.loading = false;
            return false;
          }
        }, 1000);
      } else if (flag === "32") {
        setTimeout(async () => {
          let { code } = await OutBound_batchManageState(`${this.rowId}/32`);
          if (code === "10000") {
            this.loading = false;
            this.visible = false;
            this.getBatchTableList();
          } else {
            this.loading = false;
            return false;
          }
        }, 1000);
      } else if (flag === "42") {
        setTimeout(async () => {
          let { code } = await OutBound_batchManageState(`${this.rowId}/42`);
          if (code === "10000") {
            this.loading = false;
            this.visible = false;
            this.getBatchTableList();
          } else {
            this.loading = false;
            return false;
          }
        }, 1000);
      } else if (flag === "52") {
        setTimeout(async () => {
          let { code } = await OutBound_batchManageState(`${this.rowId}/52`);
          if (code === "10000") {
            this.loading = false;
            this.visible = false;
            this.getBatchTableList();
          } else {
            this.loading = false;
            return false;
          }
        }, 1000);
      }
    },
  },
  computed: {
    columns() {
      return [
        {
          title: "订单编号",
          dataIndex: "orderNumber",
          align: "center",
          // scopedSlots: { customRender: "selfClassifyName" },
          width: "10%",
        },
        {
          title: "客户编号",
          dataIndex: "tenantId",
          align: "center",
          width: "10%",
        },
        {
          title: "物流商",
          dataIndex: "logisticsName",
          // scopedSlots: { customRender: "logisticsName" },
          align: "center",
          width: "12%",
        },
        {
          title: "运单号",
          dataIndex: "waybillNo",
          scopedSlots: { customRender: "waybillNo" },
          align: "center",
          width: "10%",
        },
        {
          title: "费用总计",
          dataIndex: "logisticsCost",
          // scopedSlots: { customRender: "logisticsCost" },
          align: "center",
          width: "12%",
        },
        {
          title: "提交时间",
          dataIndex: "createTime",
          //   scopedSlots: { customRender: "createTime" },
          align: "center",
          width: "12%",
        },
        {
          title: "状态",
          dataIndex: "applyState",
          scopedSlots: { customRender: "applyState" },
          align: "center",
          width: "10%",
        },
        {
          title: "备注",
          dataIndex: "remark",
          // scopedSlots: { customRender: "remark" },
          align: "center",
          width: 200,
        },
      ];
    },
  },
};
</script>

<style lang="less" scoped>
</style>